/**
 * 
 */
package edu.umd.clip.lm.tools;

import java.io.*;
import java.nio.channels.*;

import edu.berkeley.nlp.util.*;
import edu.umd.clip.lm.factors.FactorTuple;
import edu.umd.clip.lm.model.*;
import edu.umd.clip.lm.model.data.*;

/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public class TrainingData2Text {

	public static class Options {
        @Option(name = "-input", required = true, usage = "Training data file")
		public String input;
        @Option(name = "-config", required = true, usage = "XML config file")
		public String config;
	}
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
        OptionParser optParser = new OptionParser(Options.class);
        Options opts = (Options) optParser.parse(args, true);

		Experiment.initialize(opts.config);
		Experiment experiment = Experiment.getInstance();
		experiment.buildPrefixes();

		ReadableByteChannel channel = new FileInputStream(opts.input).getChannel();
		TrainingDataReader reader = new OnDiskTrainingDataReader(channel);
		ReadableTrainingData inputData = new ReadableTrainingData(reader);
		
		long totalCount = 0;
		long totalRecords = 0;
		long totalBlocks = 0;
		
		while(inputData.hasNext()) {
			TrainingDataBlock block = inputData.next();
			
			for(ContextFuturesPair pair : block) {
				System.out.print(pair.getContext().toString());
				System.out.print(':');

				TupleCountPair tc = pair.getFutures()[0];
				System.out.print(' ');
				System.out.print(FactorTuple.toStringNoNull(tc.tuple));
				System.out.print('*');
				System.out.print(tc.count);
				
				for(int i=1; i<pair.getFutures().length; ++i) {
					tc = pair.getFutures()[i];
					
					System.out.print(", ");
					System.out.print(FactorTuple.toStringNoNull(tc.tuple));
					System.out.print('*');
					System.out.print(tc.count);
				}
				System.out.println();
			}
			totalBlocks += 1;
			totalRecords += block.size();
			totalCount += block.getTotalCount();
		}

	}

}
